package cn.xej.easyexcel.service.impl;


import cn.xej.easyexcel.common.RespObj;
import cn.xej.easyexcel.mapper.UserMapper;
import cn.xej.easyexcel.pojo.User;
import cn.xej.easyexcel.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public JSONObject initUserTable(Integer page, Integer limit, String searchId, String searchName) {
        JSONObject jsonObject = new JSONObject();

        Map<String,Object> map = new HashMap<>();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        if(searchId != null && searchId != ""){
            map.put("searchId","%" + searchId + "%");
        }
        if(searchName != null){
            map.put("searchName","%" + searchName+ "%");
        }


        List<User> userList = userMapper.initUserTable(map);
        int count = userMapper.initUserTableCount(map);
        jsonObject.put("msg","");
        jsonObject.put("code", 0);
        jsonObject.put("data", userList);
        jsonObject.put("count", count);
        return jsonObject;
    }

    @Override
    public RespObj getUser(String userId) {
        User user = userMapper.getUserByUserId(userId);
        String roleId = userMapper.getRoleIdByUserId(userId);
        user.setRoleDesc(roleId);
        return RespObj.build(200,"success",user);
    }


    @Override
    public RespObj getUsers() {
        List<User> userList = userMapper.getUsers();
        return RespObj.build(200,"success",userList);
    }
}
